package org.hackystat.devcathlon.ui.page.matches;

import java.io.Serializable;
import java.util.Comparator;

/**
 * Comparator class used to sort Matches by name.
 * 
 * @author John Zhou
 */
public class MatchNameComparator implements Comparator<Object>, Serializable {
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  /** Boolean for ascending or descending. */
  private boolean ascending;

  /**
   * Instantiates a MatchNameComparator.
   * 
   * @param ascending The boolean for ascending or descending sort.
   */
  public MatchNameComparator(boolean ascending) {
    this.ascending = ascending;
  }

  /**
   * Override compare so that Matches can be sorted by name.
   * 
   * @param obj1 The first Match to be sorted.
   * @param obj2 The second Match to be sorted.
   * @return compare A negative, zero, or a positive integer corresponding to less than, equal to,
   * or greater than.
   */
  public int compare(Object obj1, Object obj2) {
    String a = (String) obj1;
    String b = (String) obj2;
    int compare = a.compareTo(b);
    if (!ascending) {
      compare *= -1;
    }
    return compare;
  }

}
